package com.example.librarysystem.Service;

import com.example.librarysystem.Dao.AdminDao;
import com.example.librarysystem.Dao.BooksDao;
import com.example.librarysystem.entity.Admin;
import com.example.librarysystem.entity.AdminBook;
import com.example.librarysystem.entity.Books;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("adminService")
public class AdminServiceImpl implements AdminService{
    @Autowired
    private AdminDao adminDao;

    @Override
    public Integer selectCount(){
        return adminDao.selectCount();
    }

    @Override
    public Admin selectByName(String name){
        return adminDao.selectByName(name);
    }

    @Override
    public List<Books> selectBookAfter(AdminBook adminBook, int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            adminBook.setCurrentPage(startCurrentPage);
            adminBook.setPageSize(pageSize);
            return adminDao.selectBookAfter(adminBook);
        }
    }

    @Override
    public List<Books> selectBookBefore(AdminBook adminBook, int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            adminBook.setCurrentPage(startCurrentPage);
            adminBook.setPageSize(pageSize);
            return adminDao.selectBookBefore(adminBook);
        }
    }

    @Override
    public List<Books> selectBookBetween(AdminBook adminBook, int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            adminBook.setCurrentPage(startCurrentPage);
            adminBook.setPageSize(pageSize);
            return adminDao.selectBookBetween(adminBook);
        }
    }

    @Override
    public List<Books> selectBook(AdminBook adminBook, int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            adminBook.setCurrentPage(startCurrentPage);
            adminBook.setPageSize(pageSize);
            return adminDao.selectBook(adminBook);
        }
    }

    @Override
    public int selectBookAfterCount(AdminBook adminBook){
        return adminDao.selectBookAfterCount(adminBook);
    }
    @Override
    public int selectBookBeforeCount(AdminBook adminBook){
        return adminDao.selectBookBeforeCount(adminBook);
    }
    @Override
    public int selectBookBetweenCount(AdminBook adminBook){
        return adminDao.selectBookBetweenCount(adminBook);
    }
    @Override
    public int selectBookCount(AdminBook adminBook){
        return adminDao.selectBookCount(adminBook);
    }
}
